<template>
  <!-- 日志查看-登录日志 -->
  <div class="app-container">

    <div class="mainbox">
      <div class="filter-container">
        <el-form ref="filterFormRef" :model="filterForm" :inline="true">
          <el-form-item prop="allDate" label="执行时间">
            <el-date-picker
              v-model="allDate"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              style="max-width: 80%;"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changeTime"
            />
          </el-form-item>
          <el-form-item label="日志名称" prop="llgName">
            <el-input v-model.trim="filterForm.llgName" maxlength="30" clearable
                      placeholder="请输入内容" class="filter-item limit" @keyup.enter.native="handleFilter" />
          </el-form-item>
        </el-form>
        <div class="flex-1" />
        <el-button type="primary" @click="handleFilter">搜索</el-button>
        <el-button @click="handleReset"> 重置</el-button>
      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button v-permission="['LOGIN_LOG_DEL_BUTTON']" type="danger" icon="el-icon-delete"
                     @click="onEmpty">清空日志</el-button>
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          ref="table"
          :data="tableData"
          style="width: 100%"
          :header-cell-style="headerCellStyle"
          border
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            :key="Math.random()"
            fixed="left"
            type="index"
            label="序号"
            width="70"
            :index="table_index"
          />
          <el-table-column
            v-if="columns[0].show"
            key="userName"
            prop="userName"
            label="用户名称"
            align="center"
            width="200"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[1].show"
            key="llgName"
            prop="llgName"
            label="日志名称"
            align="center"
            width="200"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[2].show"
            key="llgSucceed"
            prop="llgSucceed"
            label="执行结果"
            align="center"
            width="200"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[3].show"
            key="createTime"
            prop="createTime"
            label="时间"
            align="center"
            width="200"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[4].show"
            key="llgMessage"
            prop="llgMessage"
            label="具体消息"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            v-if="columns[5].show"
            key="llgIpAddress"
            prop="llgIpAddress"
            label="IP"
            align="center"
            width="200"
          />
        </el-table>
        <dt-pagination
          v-show="totalRows > 0"
          :total="totalRows"
          :page.sync="filterForm.pageNo"
          :limit.sync="filterForm.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getLogLoginList, deleteAllLoginLog } from '@/framework/api/log'
import dialogColumn from '@/framework/utils/mixins/dialogColumn'

export default {
  name: 'LoginLog',
  mixins: [dialogColumn],
  data() {
    return {
      allDate: [],
      filterForm: {
        pageNo: 1,
        pageSize: 10,
        beginTime: '',
        endTime: '',
        llgName: ''
      },
      tableData: [],
      totalRows: 0,
      totalPage: 0,
      headerCellStyle: {
        backgroundColor: '#f2f2f2'
      },
      // 列信息
      columns: [
        { prop: 'userName', label: '用户名称', show: true },
        { prop: 'llgName', label: '日志名称', show: true },
        { prop: 'llgSucceed', label: '执行结果', show: true },
        { prop: 'createTime', label: '时间', show: true },
        { prop: 'llgMessage', label: '具体消息', show: true },
        { prop: 'llgIpAddress', label: 'IP', show: true }
      ]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleReset() {
      this.$refs.filterFormRef.resetFields()
      this.allDate = ''
      this.handleFilter()
    },
    changeTime(val) {
      if (!val) {
        this.allDate = []
      } else {
        this.allDate = val
      }
    },
    handleFilter() {
      this.filterForm.pageNo = 1
      this.getList()
    },
    onEmpty() {
      this.$confirm('此操作将清空登录日志, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then((_) => {
          const params = {
            beginTime: '',
            endTime: ''
          }
          if (this.allDate.length > 0) {
            params.beginTime = this.allDate[0]
            params.endTime = this.allDate[1]
          } else {
            params.beginTime = ''
            params.endTime = ''
          }
          deleteAllLoginLog().then((resp) => {
            const type = resp.success ? 'success' : 'error'
            this.$dtMessage({
              type,
              message: `${resp.message}`
            })
            this.getList()
          })
        })
        .catch((_) => {})
    },
    getList() {
      if (this.allDate) {
        this.filterForm.beginTime = this.allDate[0]
        this.filterForm.endTime = this.allDate[1]
      } else {
        this.filterForm.beginTime = ''
        this.filterForm.endTime = ''
      }
      getLogLoginList(this.filterForm).then((resp) => {
        this.tableData = [...resp.data.rows]
        this.totalRows = resp.data.totalRows
        this.totalPage = resp.data.totalPage
      })
    },
    onCurrentChange() {
      this.getList()
    },
    // 列表序号
    table_index(index) {
      return (this.filterForm.pageNo - 1) * this.filterForm.pageSize + index + 1
    }
  }
}
</script>
<style lang="scss" scoped>
  .log{
    margin: 20px;
  }
</style>
